package com.yn.xgame.gamecenter;

import com.yn.xgame.*;
import com.yn.xgame.game.GCRoomTable;
import com.yn.xgame.game.GameCenterManager;
import com.yn.xgame.game.RoomManager;
import com.yn.xgame.game.fish.GCFishEnterTable;
import com.yn.xgame.game.message.CommonGameStartNotice;
import com.yn.xgame.game.message.GameJoinTable;
import com.yn.xgame.game.message.GameStart;
import com.yn.xgame.game.message.PlayerLeave;
import com.yn.xgame.message.MqBeanMessage;
import com.yn.xgame.pb.GateClientProto;
import com.yn.xgame.pb.MessageIdProto;

/**
 * GameCenterServer
 * Created by wangboo on 17/7/15.
 */
public class GameCenterServer extends ThreadServer {

    GameCenterManager mgr;
    private String topic;

    public GameCenterServer(GameCenterManager mgr) {
        super("Center" + GameCenterApp.config.getId());
        this.mgr = mgr;
        topic = GameCenterApp.config.getTopic();
    }


    @Override
    public boolean handleMessage(IMessage message) {
        if (super.handleMessage(message)) return true;
        if (message instanceof MqBeanMessage) {
            MqBeanMessage mqMsg = (MqBeanMessage) message;
            switch (mqMsg.getMsgId()) {
                case MqMsgId.l2gc_fish_enter_table:
                    handleFishEnterTable(mqMsg);
                    break;
                case MqMsgId.common_player_game_start_notice:
                    handleEnterRoomResp(mqMsg);
                    break;
                case MqMsgId.common_player_leave:
                    handlePlayerLeave(mqMsg);
            }
        }
        return false;
    }

    // 玩家掉线
    private void handlePlayerLeave(MqBeanMessage mqMsg) {
        PlayerLeave req = (PlayerLeave) mqMsg.getBean();
        RoomManager roomManager = mgr.getCenterManager(req.game).getRoomManager(req.roomId);
        if (roomManager != null) {
            roomManager.handlePlayerLeave(req);
        }
    }

    // gamecenter -> logic 玩家加入房间成功, 更新玩家状态
    private void handleEnterRoomResp(MqBeanMessage msg) {
        CommonGameStartNotice req = (CommonGameStartNotice) msg.getBean();
        GCRoomTable table = mgr.getCenterManager(req.game).getRoomManager(req.roomId).getTable(req.tableId);
        if (table != null) {
            table.onGameStarted();
        }
    }

    // 捕鱼游戏进入桌子
    private void handleFishEnterTable(MqBeanMessage mqMsg) {
        GCFishEnterTable req = (GCFishEnterTable) mqMsg.getBean();
        RoomManager roomManager = mgr.getCenterManager(GlobalSetting.GAME_FISH).getRoomManager(req.type);
        XAssert.nonNull(roomManager, req.type);
        String gate = req.player.gateTopic;
        long sid = req.player.sid;
        if (roomManager.isFull()) {
            // 房间满了
            GateClientProto.G2CMessage resp = GateClientProto.G2CMessage.newBuilder()
                    .setMsgid(MessageIdProto.MessageId.G2C_GCEnterRoomResp)
                    .setResult(0)
                    .setWarning("房间人数已满")
                    .build();
            GameCenterApp.mqClient.sendClient(gate, sid, resp);
            return;
        }
        GCRoomTable table = roomManager.handlePlayerJoin(req.player);
        String gameTopic = GlobalSetting.GAME_FISH + roomManager.getGameServerId();
        if (table.isGameStart()) {
            // 游戏已经开始，把player信息推送到游戏服务器
            GameJoinTable join = new GameJoinTable();
            join.game = GlobalSetting.GAME_FISH;
            join.tableId = table.getTableId();
            join.player = req.player;
            GameCenterApp.mqClient.sendInter(topic, gameTopic, MqMsgId.gc2g_game_join_game,
                    join);
        } else {
            // 游戏还没开始
            if (roomManager.getMinStartSize() <= table.size()) {
                // 可以开始游戏了
                GameStart start = new GameStart();
                start.game = GlobalSetting.GAME_FISH;
                start.tableId = table.getTableId();
                start.type = req.type;
                start.list = table.getPlayerList();
                table.onGameStarted();
                GameCenterApp.mqClient.sendInter(topic, gameTopic, MqMsgId.gc2g_start_game,
                        start);
            }

        }
    }

//    private void handleEnterRoom(MqBeanMessage msg) {
//        GameCenterEnterRoom req = (GameCenterEnterRoom) msg.getBean();
//        CenterManager centerManager = mgr.getCenterManager(req.game);
//        XAssert.nonNull(centerManager, req.game);
//        RoomManager roomManager = centerManager.getRoomManager(req.roomId);
//        XAssert.nonNull(roomManager, req.roomId);
//        boolean isFull = roomManager.isFull();
//        String gate = req.player.gateTopic;
//        int sid = req.player.sid;
//        if (isFull) {
//            GateClientProto.G2CMessage resp = GateClientProto.G2CMessage.newBuilder()
//                    .setMsgid(MessageIdProto.MessageId.G2C_GCEnterRoomResp)
//                    .setResult(0)
//                    .setWarning("房间人数已满")
//                    .setGameCenterQueryResp(centerManager.getPb())
//                    .build();
//            GameCenterApp.mqClient.sendClient(gate, sid, resp);
//            return;
//        }
//        // 加入房间
//        roomManager.playerEnterRoom(req.player);
//        // 推送给客户端，玩家加入房间
//        CommonProto.RoomSimpleInfo info = roomManager.getPbRoomSimpleInfo();
//        GateClientProto.G2CMessage resp = GateClientProto.G2CMessage.newBuilder()
//                .setMsgid(MessageIdProto.MessageId.G2C_GCEnterRoomResp)
//                .setResult(1)
//                .setRoomSimpleInfo(info)
//                .build();
//        GameCenterApp.mqClient.sendClient(gate, sid, resp);
//        // 推送给logic，玩家加入房间 String game, int roomId, int userId
//        CommonGameStartNotice respToLogic = new CommonGameStartNotice(req.game, req.roomId, req.player.userId);
//        GameCenterApp.mqClient.sendInter(msg.getToTopic(), msg.getFromTopic(), MqMsgId.gamecenter_enter_room_resp,
//                respToLogic);
//    }

    // 查询大厅情况
//    private void handleGameCenterInfoQuery(MqBeanMessage mqMsg) {
//        String name = mqMsg.getBodyMapValue("game");
//        int sid = mqMsg.getBodyMapInt("sid");
//        String gate = mqMsg.getBodyMapValue("gate");
//        CenterManager centerManager = mgr.getCenterManager(name);
//        XAssert.nonNull(centerManager, name);
//        GameCenterApp.mqClient.sendClient(gate, sid, GateClientProto.G2CMessage.newBuilder()
//            .setMsgid(MessageIdProto.MessageId.G2C_GameCenterQueryResp).setResult(1)
//            .setGameCenterQueryResp(centerManager.getPb())
//            .build());
//    }
}
